package org.soenea.doitall.domain.model.invite.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.dsrg.soenea.service.logging.SQLLogger;
import org.dsrg.soenea.service.threadLocal.DbRegistry;

public class InviteFinder {
	public static final String BASE_NAME = "invite";
	public static final String TABLE = DbRegistry.getTablePrefix()+BASE_NAME;
	
	public static String SELECT_BY_ID_SQL = "SELECT i.inviteID,i.version,i.userIdInviter,"
			+ "i.groupID ,i.status,i.userIdInvited FROM "
			+ TABLE + " AS i " + "WHERE i.userIdInvited =? AND i.status=0;";
	
	public static String SELECT_BY_InviteID_SQL = "SELECT i.inviteID,i.version,i.userIdInviter,"
			+ "i.groupID ,i.status,i.userIdInvited FROM "
			+ TABLE + " AS i " + "WHERE i.inviteID =?;";
	
	public static String SELECT_BY_GROUPID_SQL = "SELECT i.inviteID,i.version,i.userIdInviter,"
			+ "i.groupID ,i.status,i.userIdInvited FROM "
			+ TABLE + " AS i " + "WHERE i.groupID=?;";
	
	public static String SELECT_ALL_SQL = "SELECT i.inviteID,i.version,i.userIdInviter,"
			+ "i.groupID ,i.status,i.userIdInvited FROM "
			+ TABLE + " AS i;";
	
	
			  
	public static ResultSet find(long userID) throws SQLException {
		 Connection con = DbRegistry.getDbConnection();
		 PreparedStatement ps = con.prepareStatement(SELECT_BY_ID_SQL);
		 ps.setLong(1, userID);
		 return SQLLogger.processQuery(ps);
	}
	
	public static ResultSet findInviteByInviteID(long inviteID) throws SQLException {
		 Connection con = DbRegistry.getDbConnection();
		 PreparedStatement ps = con.prepareStatement(SELECT_BY_InviteID_SQL);
		 ps.setLong(1, inviteID);
		 return SQLLogger.processQuery(ps);
	}
	
	public static ResultSet findAll() throws SQLException {
		Connection con = DbRegistry.getDbConnection();
		PreparedStatement ps = con.prepareStatement(SELECT_ALL_SQL);
		return SQLLogger.processQuery(ps);
	}
	
	public static ResultSet findByGroupID(Long group) throws SQLException {
		Connection con = DbRegistry.getDbConnection();
		PreparedStatement ps = con.prepareStatement(SELECT_BY_GROUPID_SQL);
		ps.setLong(1, group);
		return SQLLogger.processQuery(ps);
	}
}
